import scrapy
from pricescrapy.items import PricescrapyItem
import time
 
class PriceSpider(scrapy.Spider):
    name = "price"
    allowed_domains = ["techlabplt.com"]
    # 起始的URL地址
    start_urls=["http://www.techlabplt.com:8080/BD-PC/priceList?pageId=1"]
 
    # 在parse方法内实现爬取数据的逻辑
    def parse(self, response):
        # 获取当前页内的所有农副产品的相关信息
        price_list = response.xpath("//*[@id='tableBody']/tr")
        # 遍历price_list 获取每一项内的数据值
        for price in price_list:
            itm = PricescrapyItem()
            # 将各个字段提取出来，存入itm
            itm['top_class']=price.xpath("./td[1]/text()").extract_first()
            itm['sec_class']=price.xpath("./td[2]/text()").extract_first()
            itm['product_name']= price.xpath("./td[3]/text()").extract_first()
            itm['low_price']=price.xpath("./td[4]/text()").extract_first()
            itm['avg_price']=price.xpath("./td[5]/text()").extract_first()
            itm['high_price']= price.xpath("./td[6]/text()").extract_first()
            itm['specs'] = price.xpath("./td[7]/text()").extract_first()
            itm['origin'] = price.xpath("./td[8]/text()").extract_first()
            itm['unit'] = price.xpath("./td[9]/text()").extract_first()
            itm['update_date']= price.xpath("./td[10]/text()").extract_first()
            # 返回数据
            yield itm
 
        '''
        翻页实现
        '''
        # 翻页之前先休眠2秒
        print("---------------先休息2秒，继续爬取---------------")
        time.sleep(2)
        # 获取本页的ID，得到下一页的地址
current_url_id=response.css("input[id='currentPage']::attr(value)").extract_first()
        print("本页的ID", current_url_id)
        next_url_id = int(current_url_id) + 1
 
        # 判断当最大页等于当前页的时候 结束请求
total_url_id=response.css("input[id='totalPage']::attr(value)").extract_first()
        if current_url_id != total_url_id:
            next_url= response.urljoin("http://www.techlabplt.com:8080/BD-PC/
priceList?pageId=" + str(next_url_id))
            # next_url = response.urljoin("https://www.baidu.com")
            yield scrapy.Request(url=next_url, callback=self.parse)
